<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">
  <xsl:param name="from">1</xsl:param>
  <xsl:param name="to">40</xsl:param>
  <xsl:param name="array"/>
  <xsl:param name="organism"/>
  <xsl:param name="keyword"/>

  <xsl:output method="html"/>
  
  <xsl:template match="/">
    <xsl:variable name="from" select="experiments/@from"/>
    <xsl:variable name="to" select="experiments/@to"/>
    <xsl:variable name="total" select="experiments/@total"/>

    <xsl:variable name="the_from"><xsl:choose><xsl:when test="$from=1">0</xsl:when><xsl:otherwise><xsl:value-of select="$from"/></xsl:otherwise></xsl:choose></xsl:variable>

    <div style="font-size: 11px; font-family: 'Lucida Grande', Verdana, Arial">
      <span style="">Filter by:</span>
      organism
      <select id="ae_organism_filter" style="font-size: 11px; height: 18px; width: 150px" onchange="load_arrayexpress_experiments()">
	<option value=""><xsl:if test="$organism = ''"><xsl:attribute name="selected"/></xsl:if>No filter</option>
      </select>

      <!--
      &nbsp;&nbsp;
      array design
      <select id="ae_array_filter" style="font-size: 11px; height: 18px; width: 150px" onchange="load_arrayexpress_experiments()">
	<option value="">No filter</option>
	<optgroup label="Affymetrix">
	  <option value="HG_U95A">HG_U95A</option>
	</optgroup>
	<optgroup label="Other">
	  <option value="Homo sapiens">Homo sapiens</option>
	  <option value="Mus musculus">Mus musculus</option>
	</optgroup>
      </select>
      -->

      &#160;&#160;
      keyword
      <input value="{$keyword}" id="ae_keyword_filter" type="text" style="font-size: 11px; height: 18px; width: 150px" onchange="load_arrayexpress_experiments()"  onkeypress="if(event.keyCode=='13'){{load_arrayexpress_experiments();return false;}}"/>

      &#160;&#160;
      <input type="button" value="reset filters" style="font-size: 10px" onclick="document.getElementById('ae_organism_filter').selectedIndex=0;document.getElementById('ae_keyword_filter').value='';load_arrayexpress_experiments()"/>
    </div>
    <hr/>
    <xsl:apply-templates/>
    <hr/>

    <div style="width:100%; text-align: right; font-family: 'Lucida Grande', Verdana, Arial; font-size: 11px">Experiments <xsl:value-of select="$from"/> - <xsl:value-of select="$to"/> of <xsl:value-of select="$total"/><br/><a style="font-family: 'Lucida Grande', Verdana, Arial; font-size: 11px" href="javascript:load_arrayexpress_experiments ( 1, 40 )">First</a>&#160;<a style="font-family: 'Lucida Grande', Verdana, Arial; font-size: 11px" href="javascript:load_arrayexpress_experiments ( {$the_from} - 41, {$to} - 41 )">Prev</a>&#160;<a style="font-family: 'Lucida Grande', Verdana, Arial; font-size: 11px" href="javascript:load_arrayexpress_experiments ( {$the_from} + 41, {$to} + 41 )">Next</a>&#160;<a style="font-family: 'Lucida Grande', Verdana, Arial; font-size: 11px" href="javascript:load_arrayexpress_experiments ( {$total} - 41, {$total} )">Last</a></div>
  </xsl:template>

  <xsl:template match="experiments">
    <xsl:variable name="half">
      <xsl:if test="0=count(experiment) mod 2"><xsl:value-of select="count(experiment) div 2"/></xsl:if>
      <xsl:if test="0!=count(experiment) mod 2"><xsl:value-of select="count(experiment) div 2 + 1"/></xsl:if>
    </xsl:variable>
    <table width="100%">
      <tr>
	<xsl:choose>
	  <xsl:when test="count(experiment)=0"><td align="center">No experiments found!</td></xsl:when>
	  <xsl:otherwise>
	    <td width="50%" valign="top"><xsl:apply-templates select="experiment[position() &lt;= $half]"/></td>
	    <td width="50%" valign="top"><xsl:apply-templates select="experiment[position() &gt;  $half]"/></td>
	  </xsl:otherwise>
	</xsl:choose>
      </tr>
    </table>
  </xsl:template>

  <xsl:template match="experiment">
    <li>
      <span id="{@accnum}" style="font-family: 'Lucida Grande', Verdana, Arial; font-size: 11px" onclick='toggle_experiment("{@accnum}")' onmouseover='this.style.color="red";this.style.cursor="pointer"' onmouseout='this.style.color="black"'>
        <b><xsl:value-of select="@accnum"/></b>&#160;
        <xsl:choose>
          <xsl:when test="not(@name) or @name='' or @name=@accnum">
            <i>No description provided</i>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="@name"/>
          </xsl:otherwise>
        </xsl:choose>
      </span>
      <a style="font-family: 'Lucida Grande', Verdana, Arial; font-size: 11px;text-decoration:none;color:blue" title="Go to ArrayExpress for further detail..." target="_aeinfo" href='http://www.ebi.ac.uk/arrayexpress/query/result?queryFor=Experiment&amp;eAccession={@accnum}&amp;eSpecies=&amp;eAuthor=&amp;eArrayAccession=&amp;eExperimentType=&amp;eLaboratory=&amp;eArrayDesignName=&amp;eExperimentalFactor=&amp;ePublication=&amp;eArrayProvider=&amp;eDescription='> AE &#8594;</a>
    </li>
    <div id="desc-{@accnum}" style="display:none; border:thin solid black; background-color: #efefef; position:absolute;font-family: 'Lucida Grande', Verdana, Arial; font-size: 11px">
      <ul>
        <xsl:for-each select="description[not(starts-with(text(),'(Generated description)'))]">
	  <xsl:if test=".!=''">
            <li>
              <xsl:value-of select="." />
            </li>
          </xsl:if>
        </xsl:for-each>
        <xsl:if test="count(description[starts-with(text(),'(Generated description)')])>0">
          <ul>
            <li>samples: <xsl:value-of select="substring-before(substring-after(description[starts-with(text(),'(Generated description)')],'using '),' samples')"/> (<xsl:value-of select="substring-before(substring-after(description[starts-with(text(),'(Generated description)')],'with '),' hybridizations')"/> hybridizations)</li>
            <li>organism: <i><xsl:value-of select="substring-before(substring-after(description[starts-with(text(),'(Generated description)')],'species ['),'],')"/></i></li>
            <li>array design(s): <xsl:value-of select="substring-before(substring-after(description[starts-with(text(),'(Generated description)')],'design '),',')"/><a style="text-decoration:none;color:blue" target="_aeinfo" href='http://www.ebi.ac.uk/arrayexpress/query/details?class1=MAGE.Experiment_bioAssays&amp;queryField1=bioAssays.PhysicalBioAssay_ref.bioAssayCreation.Hybridization_ref.bioAssayCreation.array&amp;criteria1=Experiment%3D{@id}&amp;class2=MAGE.Array&amp;queryField2=arrayDesign&amp;boundaryField2;1=id&amp;boundaryRefClass2;1=MAGE.Experiment_bioAssays&amp;boundaryRefField2;1=bioAssays.PhysicalBioAssay_ref.bioAssayCreation.Hybridization_ref.bioAssayCreation.array&amp;class3=MAGE.PhysicalArrayDesign&amp;boundaryField3;1=id&amp;boundaryRefClass3;1=MAGE.Array&amp;boundaryRefField3;1=arrayDesign&amp;templateName=ArrayResultList.vt'> AE &#8594;</a></li>
          </ul>
        </xsl:if>
      </ul>
      <ul>
        <li>Data groups</li>
        <ul>
          <xsl:for-each select="bioassaydatagroups/bioassaydatagroup">
            <li>
              <span id="badg_{@id}" onclick="select_arrayexpress_badg('{@id}','{ancestor::experiment/@accnum}:{@name}');document.getElementById('desc-' + this_accnum).style.display='none';" onmouseover="this.style.cursor='pointer'" title="Click to select for analysis">
                <xsl:value-of select="@name"/> (<xsl:value-of select="@bioassay_count"/>
                <xsl:choose>
                  <xsl:when test="@is_derived=1"> experimental conditions</xsl:when>
                  <xsl:otherwise> hybridizations or scans</xsl:otherwise>
                </xsl:choose>)
              </span>
              <a style='text-decoration:none;color:blue' target='_aeinfo' href='http://www.ebi.ac.uk/arrayexpress/query/details?templateName=DataSelection.vt&amp;class=MAGE.BioAssayDataGroup&amp;criteria=experiment%3D{ancestor::experiment/@id}#{@name}_{position()}'> AE &#8594;</a>
            </li>
          </xsl:for-each>
        </ul>
      </ul>
    </div>
  </xsl:template>

</xsl:stylesheet>
